package csc.fresher.view;

import java.awt.event.ComponentEvent;
import java.io.IOException;
import java.io.Serializable;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ComponentSystemEvent;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import csc.fresher.commons.EncryptMD5;
import csc.fresher.dao.InternetAccountDAO;
import csc.fresher.entity.InternetAccount;
import csc.fresher.service.InternetLoginService;


@ManagedBean
@SessionScoped
//@Component
public class InternetLoginManageBean implements Serializable {

	public InternetLoginManageBean() {
		super();
	}

	private static final long serialVersionUID = 1L;
	
	@ManagedProperty(value="#{internetLoginServiceImpl}")
	//@Autowired
	private InternetLoginService internetLoginService;
	private InternetAccount acc;

	/*public void setInternetLoginService(
			InternetLoginService internetLoginService) {
		this.internetLoginService = internetLoginService;
	}*/

	public void setInternetLoginService(InternetLoginService internetLoginService) {
		this.internetLoginService = internetLoginService;
	}

	private String username;
	private String password;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String login() {
		if (internetLoginService.checkLock(username)) {
			FacesContext.getCurrentInstance().addMessage(null,
					new FacesMessage("Your internet banking account was locked!"));
		} else {
			acc = internetLoginService.checkLogin(username, EncryptMD5.md5(password));
			if (acc != null) {
				FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("username", acc);
				FacesContext.getCurrentInstance().getExternalContext().setSessionMaxInactiveInterval(1200);
				return "welcomepage";
			} else {
				FacesContext.getCurrentInstance().addMessage(null,new FacesMessage("Login failed!"));
			}
		}
		return "login";
	}
	
	public void checkLogin(ComponentSystemEvent e){
		InternetAccount acc = (InternetAccount) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("username");
		if(acc != null){
			try {
				FacesContext.getCurrentInstance().getExternalContext().redirect("welcomepage.jsf");
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
	}
	
	public void checkSesstionTimeout(ComponentSystemEvent evt) {
		InternetAccount acc = (InternetAccount) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("username");
		if (acc == null) {
			try {
				FacesContext.getCurrentInstance().getExternalContext().redirect("login.jsf");
			} catch (IOException e) {
				
				e.printStackTrace();
			}
		}
	}
	
	public String logout(){
		FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
		
		return "login?faces-redirect=true";
	}

	public InternetAccount getAcc() {
		return acc;
	}

	public void setAcc(InternetAccount acc) {
		this.acc = acc;
	}
}
